package com.hm.cms.websit;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hm.cms.def.AilabTypeEnum;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.TermsService;
import com.hm.cms.util.WebSitUtil;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年11月27日 下午2:24:58
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
public class AilabHandler {

	@Autowired
	private TermsService termsService;

	@Autowired
	private ArticleService articleService;
	
	private static String HOST = "http://anns.ailab.cn";

	public void handler() {
		for (AilabTypeEnum type : AilabTypeEnum.values()) {
			mainDirectory(type.getTermTaxonomy(),type.getWebTag(),"1");
			WebSitUtil.wating3();
		}
	}

	private void mainDirectory(String termTaxonomy,String uri,String operationUserId) {
		try {
			for (int page = 3; page >= 1; page--) {
				// String uri = "http://www.ailab.cn/algorithm/list_9_1.html";
				Document document = Jsoup.connect(MessageFormat.format(uri, page)).ignoreContentType(true)
						.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
				Elements elements = document.getElementsByClass("preview");
				for (Element el : elements) {
					String articleUrl = el.attr("href");
					if (!articleUrl.startsWith("http")) {
						articleUrl = HOST + articleUrl;
					}
					System.out.println(articleUrl);
					Elements previewImgs = el.getElementsByTag("img");
					String imgUri = null;
					if (CommonUtil.isNotEmpty(previewImgs)) {
						imgUri = CommonUtil.isNotEmpty(previewImgs.get(0)) ? previewImgs.get(0).attr("src")
								: null;
						if (!imgUri.startsWith("http")) {
							//http://www.ailab.cn/uploads/allimg/161214/160PW491-0.jpg
							imgUri = "http://www.ailab.cn" + imgUri;
						}
						
						System.out.println("img--->" + imgUri);
					}
					
					loadArticle(articleUrl,imgUri, termTaxonomy, operationUserId);
					WebSitUtil.wating3();
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	private void loadArticle(String uri,String imgUri,String termTaxonomy,String operationUserId) {
		try {
			if (articleService.checkArticleSourceUri(uri)) {
				return;
			}
			
			Document document = Jsoup.connect(uri).ignoreContentType(true)
					.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
			
			String description = document.select("meta[name=description]").get(0).attr("content");
			String keyStr = document.select("meta[name=keywords]").get(0).attr("content");
			System.out.println(description);
			System.out.println(keyStr);
			
			List<String> termIds = new ArrayList<String>();
			String[] keys = keyStr.split(",");
			for (String key : keys) {
				if(CommonUtil.isEmpty(key)) {
					continue;
				}
				HcmsTermsVo hcmsTermsVo = termsService.getHcmsTermsByTermName(key);
				if (CommonUtil.isNotEmpty(hcmsTermsVo)) {
					termIds.add(hcmsTermsVo.getId());
					continue;
				}

				HcmsTermsVo termsVo = new HcmsTermsVo();
				termsVo.setTermName(key);
				termsVo.setTermCode(key);
				termsVo.setTermOrder(-1);
				termIds.add(termsService.addTerms(termsVo, operationUserId));
			}
			
			Elements titleTag = document.getElementsByTag("title");
			String title = titleTag.text().split("-")[0];
			
			Element articleContent = document.getElementById("Article");
			{
				Elements reEls = articleContent.getElementsByClass("summary");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				reEls = articleContent.getElementsByClass("fang");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				reEls = articleContent.getElementsByClass("pcb");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				reEls = articleContent.getElementsByClass("bdsharebuttonbox");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				reEls = articleContent.getElementsByClass("rc");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				reEls = articleContent.getElementsByClass("jh_76090");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				reEls = articleContent.getElementsByClass("xg_rc");
				if(CommonUtil.isNotEmpty(reEls)) {
					reEls.remove();
				}
				Element reEl = articleContent.getElementById("button_zanzhu_1");
				if(CommonUtil.isNotEmpty(reEl)) {
					reEl.remove();
				}
				reEl = articleContent.getElementById("button_zanzhu_2");
				if(CommonUtil.isNotEmpty(reEl)) {
					reEl.remove();
				}
			}
			
			String content = articleContent.html();
			System.out.println(content);
			
			if(StringUtil.isBlankOrNull(description)) {
            	description = StringUtil.dislodgeAllBlank(WebUtil.clearHtmlTags(content)).substring(0, 150);
            }
            
			HcmsArticleVo articleVo = new HcmsArticleVo();
			{
				articleVo.setTermIds(termIds);
				articleVo.setTermTayIds(Arrays.asList(termTaxonomy));
				articleVo.setArticleContent(content);
				articleVo.setBreviaryIcon(imgUri);
				articleVo.setAuthorName("马德超");
				articleVo.setReleaseTime(System.currentTimeMillis());
				articleVo.setTitle(title);
				articleVo.setKeywords(StringUtil.isBlankOrNull(keyStr)?title:keyStr);
				articleVo.setSourceName("中国人工智能网");
				articleVo.setSourceUri(uri);
				articleVo.setArticleOrder((int) articleService.getArtilcleMaxOrder());
				articleVo.setArticleExcerpt(description);
			}
			articleService.addArticle(articleVo, operationUserId);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	public static void main(String[] args) throws Exception {
		String uri = "http://anns.ailab.cn/article-84586.html";
		Document document = Jsoup.connect(uri).ignoreContentType(true)
				.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
		
		String description = document.select("meta[name=description]").get(0).attr("content");
		String keyStr = document.select("meta[name=keywords]").get(0).attr("content");
		System.out.println(description);
		System.out.println(keyStr);
		
		Elements titleTag = document.getElementsByTag("title");
		String title = titleTag.text().split("-")[0];
		
		Element articleContent = document.getElementById("Article");
		{
			Elements reEls = articleContent.getElementsByClass("summary");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
//			reEls = articleContent.getElementsByClass("content_z");
			reEls = articleContent.getElementsByClass("fang");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
			reEls = articleContent.getElementsByClass("pcb");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
			reEls = articleContent.getElementsByClass("bdsharebuttonbox");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
			reEls = articleContent.getElementsByClass("rc");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
			reEls = articleContent.getElementsByClass("jh_76090");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
			reEls = articleContent.getElementsByClass("xg_rc");
			if(CommonUtil.isNotEmpty(reEls)) {
				reEls.remove();
			}
			Element reEl = articleContent.getElementById("button_zanzhu_1");
			if(CommonUtil.isNotEmpty(reEl)) {
				reEl.remove();
			}
			reEl = articleContent.getElementById("button_zanzhu_2");
			if(CommonUtil.isNotEmpty(reEl)) {
				reEl.remove();
			}
		}
		System.out.println(articleContent.html());
	}
}
